package com.util.shell;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.util.common.FilePath;
import com.util.system.SystemUtils;

public class TomcatShell {
	
	private static final String TOMCAT_STARTUP = "startup.bat";
	private static final String TOMCAT_SHUTDOWN = "shutdown.bat";
	//linux 重启命令
	private static final String LINUX_TOMCAT_STARTUP = "startup.sh";
	private static final String LINUX_TOMCAT_SHUTDOWN = "shutdown.sh";
	
	/**
	 * 获得tomcat的根路径。
	 * 无法获得返回""。
	 * @return
	 */
	public static String tomcatRootPath(){
		String webRoot = FilePath.getWEBRoot();
		if(webRoot.indexOf("/webapps/") > -1){
			return webRoot.substring(0,webRoot.indexOf("/webapps/"));
		}else{
			return System.getProperty("catalina.base");
		}
	}
	
	/**
	 * 开启tomcat运行。
	 * 返回开启的时间。
	 * @return
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static int startUp() throws IOException, InterruptedException{
		if(SystemUtils.isLinux()){
			return linuxStartUp();
		}else{
			return winStartUp();
		}
	}
	
	public static int linuxStartUp() throws InterruptedException, IOException{
		String path = tomcatRootPath() + "/bin/"+LINUX_TOMCAT_STARTUP;
		String[] command = {"sh","-c",path};
		return Runtime.getRuntime().exec(command).waitFor();
	}
	
	public static int winStartUp() throws InterruptedException, IOException{
		String path = tomcatRootPath() + "\\bin\\"+TOMCAT_STARTUP;
//		String[] command = {"sh","-c",path};
//		String[] evn = {"JAVA_HOME=D:\\JAVAEE\\Java\\jdk1.6.0_12","CATALINA_HOME=D:\\tomcat\\tomcat-6.0.26"};
//		return Runtime.getRuntime().exec(path).waitFor();
		return 0;
	}
	
	/**
	 * 重启tomcat运行。
	 * 返回重启的时间。
	 * @return
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static int reboot() throws IOException, InterruptedException{
		if(SystemUtils.isLinux()){
			return linuxReboot();
		}else{
			return winReboot();
		}
	}
	
	public static int linuxReboot() throws InterruptedException, IOException{
		String tomcatShellPath = FilePath.getAbsolutePathWithClass(TomcatShell.class) + "tom_reboot.sh";
		
		File file = new File(tomcatShellPath);
		List<String> stempList = FileUtils.readLines(file, "UTF-8");
		for(int i = 0; i < stempList.size(); i++){
			if(StringUtils.isBlank(stempList.get(i)) && stempList.get(i).indexOf("TOMCATPATH=") > -1){
				stempList.set(i,"TOMCATPATH=" + tomcatRootPath());
				break;
			}
		}
		FileUtils.writeLines(file, "UTF-8", stempList);
		
		String[] command = {"sh","-c",tomcatShellPath};
		Runtime.getRuntime().exec(command);
		
		return 0;
	}
	
	public static int winReboot() throws InterruptedException, IOException{
		return 0;
	}
	
	/**
	 * 关闭tomcat运行。
	 * 返回关闭的时间。
	 * @return
	 * @throws InterruptedException
	 * @throws IOException
	 */
	public static int shutDown() throws InterruptedException, IOException{
		if(SystemUtils.isLinux()){
			return linuxShutDown();
		}else{
			return winShutDown();
		}
	}
	
	public static int linuxShutDown() throws InterruptedException, IOException{
		String path = tomcatRootPath() + "/bin/" + LINUX_TOMCAT_SHUTDOWN;
		String[] command = {"sh","-c",path};
		return Runtime.getRuntime().exec(command).waitFor();
	}
	
	public static int winShutDown() throws InterruptedException, IOException{
		String path = tomcatRootPath() + "\\bin\\"+TOMCAT_SHUTDOWN;
		return 0;
	}
}